//
//  SceneryDetailController.m
//  lv678
//
//  Created by apple on 15/9/8.
//  Copyright (c) 2015年 lampshade. All rights reserved.
//  景点详情

#import "SceneryDetailController.h"
#import "UIImageView+WebCache.h"
#import "AFNetworking.h"
#import "SceneryDetailModel.h"
#import "SceneryDetailCell.h"
#import "CommonLoading.h"
#import "SceneryOrderController.h"

@interface SceneryDetailController ()<SceneryDetailCellDelegate>
@property (nonatomic, strong) UIView *headerView;//tableview header视图
@property (nonatomic, strong) NSMutableArray *sceneryDetailArry;//可变数组
@property (nonatomic, weak) UIView *loadingView;//加载视图
@property (nonatomic, strong) NSDictionary *dict;//字典
@property (nonatomic, assign) int MaxQuantity;//最大可购买数量
@property (nonatomic, assign) int MinQuantity;//最小可购买数量
@end

@implementation SceneryDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"景点详情";
    self.tableView.tableHeaderView = self.headerView;
    [self initTableView];
    [self getData];
}

- (void)initTableView{
    self.sceneryDetailArry = [NSMutableArray array];
    
    UIView *loadingView = [[CommonLoading initCommonLoading]refreshLoading];
    self.loadingView = loadingView;
    [self.tableView addSubview:loadingView];
    [self.tableView bringSubviewToFront:loadingView];
    
    [self.tableView setShowsVerticalScrollIndicator:NO];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark - 设置TableView的header视图
- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 120)];
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 80, 60)];
        [image sd_setImageWithURL:[NSURL URLWithString:self.detailModel.Image] placeholderImage:nil];
        
        UILabel *introduceOfScenery = [[UILabel alloc]initWithFrame:CGRectMake(90, 0, self.tableView.frame.size.width-95, 30)];
        introduceOfScenery.text = @"景点介绍";
        
        UILabel *addressOfScenery = [[UILabel alloc]initWithFrame:CGRectMake(90, 20, self.tableView.frame.size.width-95, 60)];
        addressOfScenery.text = self.detailModel.Address;
        addressOfScenery.lineBreakMode = NSLineBreakByWordWrapping;
        addressOfScenery.numberOfLines = 0;
        addressOfScenery.font = [UIFont systemFontOfSize:14.0];
        
        UILabel *productManagerRecommand = [[UILabel alloc]initWithFrame:CGRectMake(5, 60, self.tableView.frame.size.width, 60)];
        productManagerRecommand.text = self.detailModel.ProductManagerRecommand;
        productManagerRecommand.font = [UIFont systemFontOfSize:14.0];
        productManagerRecommand.lineBreakMode = NSLineBreakByWordWrapping;
        productManagerRecommand.numberOfLines = 0;
        [_headerView addSubview:image];
        [_headerView addSubview:introduceOfScenery];
        [_headerView addSubview:addressOfScenery];
        [_headerView addSubview:productManagerRecommand];
        
    }
    return _headerView;
}
#pragma mark -请求接口获取数据
- (void)getData{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"UrlList.plist" ofType:nil];
    NSDictionary *temp = [NSDictionary dictionaryWithContentsOfFile:path];
    NSString *url = [temp objectForKey:@"viewspotapp"];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    NSDictionary *data = @{@"serviceName":@"TicketSenicSpotInfo",@"queryParameters":
                               @{@"ResponseDataType":@"0",@"DistributionChannel":@"9",@"ID":@[
                                                                                             [NSNumber numberWithInt:self.detailModel.ID]
                                                                                             ]
}};
    [manager POST:url parameters:data success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.loadingView removeFromSuperview];
        NSDictionary *temp = [[[[[[[[responseObject objectForKey:@"ScenicSpotInfoResponse"]objectForKey:@"ScenicSpotList"]objectForKey:@"ScenicSpotDTO"]objectForKey:@"ProductList"]objectForKey:@"ProductDTO"]objectForKey:@"ProductAddInfoList"]objectForKey:@"ResourceList"]objectForKey:@"ResourceDTO"];
        for (NSDictionary *dict in temp) {
            SceneryDetailModel *model = [SceneryDetailModel sceneryDetailWithDict:dict];
            [self.sceneryDetailArry addObject:model];
        }

        [self.tableView reloadData];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.loadingView removeFromSuperview];
        UIView *noDataTips = [[CommonLoading initCommonLoading]noDataTips];
        [self.tableView addSubview:noDataTips];
        
    }];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.sceneryDetailArry.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 162;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identity = @"sceneryDeailCell";
    SceneryDetailModel *model = self.sceneryDetailArry[indexPath.row];
    SceneryDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:identity];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"SceneryDetailCell" owner:self options:nil]lastObject];
    }
    //将当前控制器设置为cell的代理
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setData:model];
    return cell;
}

- (void)pushViewController:(UIViewController *)viewController sendValue:(NSDictionary *)dict{
    
    //重新封装字典的值
    NSString *ProductID = [[self.detailModel.ProductListItemList objectForKey:@"ProductListItemDTO"]valueForKey:@"ID"];
    NSString *PassportType = [dict valueForKey:@"PassportType"];
    NSString *sceneryName = [dict valueForKey:@"sceneryName"];
    NSString *ResourceID = [dict valueForKey:@"ResourceID"];
    self.dict = @{@"ProductID":ProductID,@"sceneryName":sceneryName,@"ResourceID":ResourceID,@"PassportType":PassportType};
    
    SceneryOrderController *orderController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"sceneryOrder"];
    orderController.resource = [self.dict copy];
    [self.navigationController pushViewController:orderController animated:YES];
}

@end
